package cn.ming.wfchat.kit.conversation.ext;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;

import com.lcw.library.imagepicker.ImagePicker;
import com.lcw.library.imagepicker.activity.ImagePickerActivity;
import com.lcw.library.imagepicker.utils.MediaFileUtil;

import java.io.File;
import java.util.List;

import cn.ming.wfchat.kit.R;
import cn.ming.wfchat.kit.annotation.ExtContextMenuItem;
import cn.ming.wfchat.kit.conversation.ext.core.ConversationExt;
import cn.ming.wfchat.kit.third.utils.ImageUtils;
import cn.ming.wfchat.kit.third.utils.UIUtils;
import cn.ming.wfchat.kit.utils.GlideLoader;
import cn.wildfirechat.model.Conversation;
import cn.wildfirechat.remote.ChatManager;

public class ImageExt extends ConversationExt {

    /**
     * @param containerView 扩展view的container
     * @param conversation
     */
    @ExtContextMenuItem
    public void pickImage(View containerView, Conversation conversation) {
//        Intent intent = ImagePicker.picker().showCamera(true).enableMultiMode(9).buildPickIntent(activity);
//        startActivityForResult(intent, 100);
        ImagePicker.getInstance()
                .showImage(true)//设置是否展示图片
                .showVideo(true)//设置是否展示视频
                .setSingleType(true)//设置图片视频不能同时选择
                .setMaxCount(9)//设置最大选择图片数目(默认为1，单选)
                .setImageLoader(new GlideLoader(activity));
        Intent intent = new Intent(activity, ImagePickerActivity.class);
        startActivityForResult(intent, 100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (data != null) {
                ChatManager.Instance().getWorkHandler().post(() -> {
                    List<String> imagePaths = data.getStringArrayListExtra(ImagePicker.EXTRA_SELECT_IMAGES);
                    if (imagePaths == null || imagePaths.isEmpty()) {
                        return;
                    }
                    if (MediaFileUtil.isVideoFileType(imagePaths.get(0))) {
                        //如果是视频
                        UIUtils.postTaskSafely(() -> messageViewModel.sendVideoMsg(conversation, new File(imagePaths.get(0))));
                    } else {
                        //如果是图片
                        for (String imageItem : imagePaths) {

                            File imageFileThumb;
                            File imageFileSource = null;
                            imageFileSource = ImageUtils.compressImage(imageItem);
                            imageFileThumb = ImageUtils.genThumbImgFile(imageItem);
                            if (imageFileThumb == null) {
                                Log.e("ImageExt", "gen image thumb fail");
                                return;
                            }
                            File finalImageFileSource = imageFileSource;
                            UIUtils.postTaskSafely(() -> messageViewModel.sendImgMsg(conversation, imageFileThumb, finalImageFileSource));
                        }

                    }

                });

            }
        }
    }

    @Override
    public int priority() {
        return 100;
    }

    @Override
    public int iconResId() {
        return R.mipmap.ic_func_pic;
    }

    @Override
    public String title(Context context) {
        return "照片";
    }

    @Override
    public String contextMenuTitle(Context context, String tag) {
        return title(context);
    }
}
